//
//  YYLinkShareManager.m
//  YYLinkShareView
//
//  Created by 云眼科技 on 16/6/2.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "YYLinkShareManager.h"

#import "YYLinkShareView.h"
#import "AppDelegate.h"

#import "YYLinkShareAlertView.h"



@interface YYLinkShareManager ()<YYLinkShareViewDelegate, TencentSessionDelegate>{
    /** 消息体 */
    NSDictionary *_messageObject;
    NSString     *_text;
    UIImage      *_shareImage;
    
    NSString     *_imagePath;
    NSString     *_gifPath;
    
    
    TencentOAuth *_tencentOAuth;
    BOOL          _isGif;
}

@property (nonatomic, strong) YYLinkShareView *shareView;
@property (nonatomic, strong) NSMutableArray  *iconArray;

@end

@implementation YYLinkShareManager





#pragma mark -- ***************************** | 注册App |  *****************************--
-(void)registerAppKeyWithPlatform:(platformType)platform AppKey:(NSString *)AppKey{
    switch (platform) {
        case platformTypeWeibo:
            //微博
            [WeiboSDK enableDebugMode:YES];
            [WeiboSDK registerApp:AppKey];
            break;
        case platformTypeQQ:
            _tencentOAuth = [[TencentOAuth alloc] initWithAppId:AppKey andDelegate:self]; 
            break;
        case platformTypeWeChat:
            [WXApi registerApp:AppKey withDescription:@"foo"];
            break;
        default:
            break;
    }
}


#pragma mark -- ***************************** | 分享消息 |  *****************************--





-(void)shareMessageWIthText:(NSString *)text imagePath:(NSString *)imagePath{
    _text = text;
    _imagePath = imagePath;
    _isGif = NO;
    [self resetShareView];
    [self.shareView showShareViewAnimated:YES];
}



-(void)shareMessageWithText:(NSString *)text thumbImagePath:(NSString *)thumbimagePath gifPath:(NSString *)gifPath{
    _text = text;
    _imagePath = thumbimagePath;
    _isGif = YES;
    _gifPath = gifPath;
    
    [self resetShareView];
    [self.shareView showShareViewAnimated:YES];
}


-(void)resetShareView{
    self.shareView = nil;
    self.iconArray = nil;
}


#pragma mark -- ***************************** | ShareViewDelegate |  *****************************--
-(void)didSelectView:(YYLinkShareView *)shareView withPlatforType:(platformType)type{

    switch (type) {
        case platformTypeQQ:
            [self shareToQQ];
            break;
        case platformTypeWeibo:
            [self shareToWeibo];
            break;
        case platformTypeCircle:
            [self shareToCircle];
            break;
        case platformTypeWeChat:
            [self shareToWeChat];
            break;
        default:
            break;
    }

}

#pragma mark -- ***************************** | QQ |  *****************************--
-(void)shareToQQ{
    
    if (_isGif == YES) {
        
        NSData* data = [NSData dataWithContentsOfFile:_gifPath];
        QQApiImageObject   *img = [QQApiImageObject objectWithData:data previewImageData:data title:@"111" description:_text];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:img];
        QQApiSendResultCode sentCode = [QQApiInterface sendReq:req];
        NSString *sentMsg = [[YYLinkShareHandler defaultHandler] handleQQStateWithCode:sentCode];
        [YYLinkShareAlertView showMessageWithTitle:@"分享完成" message:sentMsg withStyle:YYLinkAlertStyleOK];
    }else{
        
        NSData *previewData = [NSData dataWithContentsOfFile:_imagePath];
        QQApiImageObject *img  = [QQApiImageObject objectWithData:previewData previewImageData:previewData title:@" 零克gif图片" description:_text];
        SendMessageToQQReq *req  = [SendMessageToQQReq reqWithContent:img];
        QQApiSendResultCode sentCode = [QQApiInterface sendReq:req];
        NSString *sentMsg = [[YYLinkShareHandler defaultHandler] handleQQStateWithCode:sentCode];
        [YYLinkShareAlertView showMessageWithTitle:@"分享完成" message:sentMsg withStyle:YYLinkAlertStyleOK];
    }
    

}







#pragma mark -- ***************************** | 微博 |  *****************************--
-(void)shareToWeibo{
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"https://api.weibo.com/oauth2/default.html";
    authRequest.scope = @"all";
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare]];
    request.userInfo = @{@"分享信息来源于": @"云眼科技零克"};
    [WeiboSDK sendRequest:request];
}


- (WBMessageObject *)messageToShare{
    WBMessageObject *message = [WBMessageObject message];
    message.text = _text;
    WBImageObject *imgObj = [WBImageObject object];
    
    imgObj.imageData = [NSData dataWithContentsOfFile:_imagePath];
    
    message.imageObject = imgObj;
    return message;
}


#pragma mark -- **************************| 微信好友 |  *****************************--
-(void)shareToWeChat{
    
    if (_isGif == YES) {
        WXMediaMessage *message = [WXMediaMessage message];
        [message setThumbImage:[UIImage imageWithContentsOfFile:_imagePath]];
        WXEmoticonObject *obj = [WXEmoticonObject object];
        NSData *dadadadata = [NSData dataWithContentsOfFile:_gifPath];
        obj.emoticonData = dadadadata;
        message.mediaObject = obj;
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
        req.bText = NO;
        req.message = message;
        req.scene = 0;
        [WXApi sendReq:req];
        
    }else{
        WXMediaMessage *message = [WXMediaMessage message];
        message.ThumbImage = [UIImage imageWithContentsOfFile:_imagePath];
        WXImageObject *obj = [WXImageObject object];
        NSData *ddd = [NSData dataWithContentsOfFile:_imagePath];
        obj.imageData = ddd; 
        message.mediaObject = obj;
        
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
        req.bText = NO;
        req.message = message;
        req.scene = 0;
        [WXApi sendReq:req];
    }
}


#pragma mark -- ***************************** | 朋友圈 |  *****************************--
-(void)shareToCircle{
    WXMediaMessage *message = [WXMediaMessage message];
    message.ThumbImage = [UIImage imageNamed:@"weixin"];
    WXImageObject *obj = [WXImageObject object];
    NSData *ddd = [NSData dataWithContentsOfFile:_imagePath];
    obj.imageData = ddd; 
    message.mediaObject = obj;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
    req.bText = NO;
    req.message = message;
    req.scene = 1;
    [WXApi sendReq:req];
}




#pragma mark -- ***************************** | weibo |  *****************************--
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request{
}

//回调信息
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{
    NSString *message = nil;
    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class]){
        message = [[YYLinkShareHandler defaultHandler] handleWeiboStateWithCode:response.statusCode];
    }
    [YYLinkShareAlertView showMessageWithTitle:@"分享完成" message:message withStyle:YYLinkAlertStyleOK];
}




#pragma mark -- ***************************** | WXApiDelegate |  *****************************--

-(void)onReq:(BaseReq*)req{
}

-(void)onResp:(BaseResp*)resp{
    NSString *msg = [[YYLinkShareHandler defaultHandler] handleWeChatStateWithCode:resp.errCode];
    [YYLinkShareAlertView showMessageWithTitle:@"提示" message:msg withStyle:YYLinkAlertStyleOK];
}




#pragma mark -- ***************************** | Tencent |  *****************************--
/** 登录成功后的回调 */
- (void)tencentDidLogin{
}
/** 登录失败后的回调 代表用户是否主动退出登录 */
- (void)tencentDidNotLogin:(BOOL)cancelled{
}
/** 登录时网络有问题的回调 */
- (void)tencentDidNotNetWork{
}




#pragma mark -- ***************************** | getter |  *****************************--

-(YYLinkShareView *)shareView{

     if (!_shareView){
        CGFloat kWidth = [UIScreen mainScreen].bounds.size.width;
        CGFloat kHeight = [UIScreen mainScreen].bounds.size.height;
        _shareView = [[YYLinkShareView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) iconArray:self.iconArray];
        _shareView.delegate = self;
    }
    return _shareView;
}


-(NSMutableArray *)iconArray{
    if (!_iconArray) {
        _iconArray = [[NSMutableArray alloc] init];
        
        if ([TencentOAuth iphoneQQInstalled]) {
            [_iconArray addObject:@"qq"];
        }
        if (_isGif == NO) {
            [_iconArray addObject:@"weibo"];
            [_iconArray addObject:@"circle"];
        }
        if ([WXApi isWXAppInstalled] == YES) {
            [_iconArray addObject:@"weixin"];
        }
    }
    return _iconArray;
}

#pragma mark -- ***************************** | 单例 |  *****************************--
+(YYLinkShareManager *)shareManager{
    static YYLinkShareManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[YYLinkShareManager alloc] init];
        
    });
    return manager;
}

#pragma mark -- ***************************** | init |  *****************************--


@end
